package summary;

import java.util.Stack;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/10 17:11
 */
public class Title150 {

    public static int evalRPN(String[] tokens) {
        Stack<Integer> nums = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            if (tokens[i].matches("[+-/*]")) {
                Integer num2 = nums.pop();
                Integer num1 = nums.pop();
                if ("*".equals(tokens[i])) {
                    nums.push(num1 * num2);
                } else if ("/".equals(tokens[i])) {
                    nums.push(num1 / num2);
                } else if ("+".equals(tokens[i])) {
                    nums.push(num1 + num2);
                } else {
                    nums.push(num1 - num2);
                }
            } else {
                nums.push(Integer.valueOf(tokens[i]));
            }
        }
        return nums.peek();
    }

    public static void main(String[] args) {
        String[] tokens = {"+","-","*","/","+"};
        evalRPN(tokens);
    }
}
